//
//  NMMedia.swift
//  NetEaseMusic
//
//  Created by SAGESSE on 2020/2/25.
//  Copyright © 2020 SAGESSE. All rights reserved.
//

import UIKit

class NMMedia {
    
    typealias CustomKey = JSONDecoderEx.CustomKey
    
    class Artist: Decodable {
        
        var id: Int
        var name: String
        
        //                    "name": "Mr. Miss",
        //                    "id": 221425,
        //                    "picId": 0,
        //                    "img1v1Id": 0,
        //                    "briefDesc": "",
        //                    "albumSize": 0,
        //                    "alias": [],
        //                    "trans": "",
        //                    "musicSize": 0
        
        var picUrl: URL?
        var img1v1Url: URL?
        
        var alias: [String]?
        
        var trans: String?
    }

    
    class Song: Decodable {
        
        var id: Int
        
        var name: String
        
        var alias: [String]?
        var transNames: [String]?

        var artists: [Artist] = []
        
        
        
        var album: Album?
        //            "song": {
        //                "name": "You Were Meant For Me",
        //                "id": 1424479961,
        //                "position": 0,
        //                "alias": ["电视剧《我在北京等你》插曲"],
        //                "status": 0,
        //                "fee": 8,
        //                "copyrightId": 753018,
        //                "disc": "01",
        //                "no": 2,
        //                "artists": [{
        //                    "name": "Mr. Miss",
        //                    "id": 221425,
        //                    "picId": 0,
        //                    "img1v1Id": 0,
        //                    "briefDesc": "",
        //                    "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                    "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                    "albumSize": 0,
        //                    "alias": [],
        //                    "trans": "",
        //                    "musicSize": 0
        //                }],
        //                "album": {
        //                    "name": "那点情话/You Were Meant For Me",
        //                    "id": 85839370,
        //                    "type": "EP/Single",
        //                    "size": 0,
        //                    "picId": 109951164722225814,
        //                    "blurPicUrl": "http://p2.music.126.net/x5AHmM9jXuBonZPERMQz1Q==/109951164722225814.jpg",
        //                    "companyId": 0,
        //                    "pic": 109951164722225814,
        //                    "picUrl": "http://p2.music.126.net/x5AHmM9jXuBonZPERMQz1Q==/109951164722225814.jpg",
        //                    "publishTime": 1582128000000,
        //                    "description": "",
        //                    "tags": "",
        //                    "company": "智慧大狗 × 天娱传媒",
        //                    "briefDesc": "",
        //                    "artist": {
        //                        "name": "",
        //                        "id": 0,
        //                        "picId": 0,
        //                        "img1v1Id": 0,
        //                        "briefDesc": "",
        //                        "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                        "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                        "albumSize": 0,
        //                        "alias": [],
        //                        "trans": "",
        //                        "musicSize": 0
        //                    },
        //                    "songs": [],
        //                    "alias": ["电视剧《我在北京等你》插曲"],
        //                    "status": 0,
        //                    "copyrightId": 753018,
        //                    "commentThreadId": "R_AL_3_85839370",
        //                    "artists": [{
        //                        "name": "Mr. Miss",
        //                        "id": 221425,
        //                        "picId": 0,
        //                        "img1v1Id": 0,
        //                        "briefDesc": "",
        //                        "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                        "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                        "albumSize": 0,
        //                        "alias": [],
        //                        "trans": "",
        //                        "musicSize": 0
        //                    }],
        //                    "img80x80": "http://p1.music.126.net/x5AHmM9jXuBonZPERMQz1Q==/109951164722225814.jpg?param=80x80x1"
        //                },
        //                "starred": false,
        //                "popularity": 100.0,
        //                "score": 100,
        //                "starredNum": 0,
        //                "duration": 180283,
        //                "playedNum": 0,
        //                "dayPlays": 0,
        //                "hearTime": 0,
        //                "ringtone": "",
        //                "crbt": null,
        //                "audition": null,
        //                "copyFrom": "",
        //                "commentThreadId": "R_SO_4_1424479961",
        //                "rtUrl": null,
        //                "ftype": 0,
        //                "rtUrls": [],
        //                "copyright": 0,
        //                "bMusic": {
        //                    "name": null,
        //                    "id": 4514135008,
        //                    "size": 2885634,
        //                    "extension": "mp3",
        //                    "sr": 44100,
        //                    "dfsId": 0,
        //                    "bitrate": 128000,
        //                    "playTime": 180283,
        //                    "volumeDelta": -19928.0
        //                },
        //                "mp3Url": null,
        //                "rtype": 0,
        //                "rurl": null,
        //                "mvid": 0,
        //                "mMusic": {
        //                    "name": null,
        //                    "id": 4514135007,
        //                    "size": 4328429,
        //                    "extension": "mp3",
        //                    "sr": 44100,
        //                    "dfsId": 0,
        //                    "bitrate": 192000,
        //                    "playTime": 180283,
        //                    "volumeDelta": -21710.0
        //                },
        //                "lMusic": {
        //                    "name": null,
        //                    "id": 4514135008,
        //                    "size": 2885634,
        //                    "extension": "mp3",
        //                    "sr": 44100,
        //                    "dfsId": 0,
        //                    "bitrate": 128000,
        //                    "playTime": 180283,
        //                    "volumeDelta": -19928.0
        //                },
        //                "hMusic": {
        //                    "name": null,
        //                    "id": 4514135006,
        //                    "size": 7214019,
        //                    "extension": "mp3",
        //                    "sr": 44100,
        //                    "dfsId": 0,
        //                    "bitrate": 320000,
        //                    "playTime": 180283,
        //                    "volumeDelta": -24350.0
        //                }
        //            }
    }
    class Album: Decodable {
        
        var id: Int
        
        var name: String

        var type: String
        
        var description: String?
        
        var tags: String?
        var company: String?
        
        var blurPicUrl: URL?
        var picUrl: URL?
        
        var artist: Artist
        
        var artists: [Artist]?
        //            "album": {
        //                "name": "Skin",
        //                "id": 34700845,
        //                "type": "专辑",
        //                "size": 16,
        //                "picId": 109951164430276051,
        //                "blurPicUrl": "http://p2.music.126.net/N9QDa3qlkRJ3IyFHVY07cg==/109951164430276051.jpg",
        //                "companyId": 0,
        //                "pic": 109951164430276051,
        //                "picUrl": "http://p2.music.126.net/N9QDa3qlkRJ3IyFHVY07cg==/109951164430276051.jpg",
        //                "publishTime": 1464278400000,
        //                "description": "",
        //                "tags": "",
        //                "company": "Future Classic",
        //                "briefDesc": "",
        //                "artist": {
        //                    "name": "Flume",
        //                    "id": 361505,
        //                    "picId": 109951164017636713,
        //                    "img1v1Id": 0,
        //                    "briefDesc": "",
        //                    "picUrl": "http://p2.music.126.net/vGH3ibOjHsTGDIgX4CT35g==/109951164017636713.jpg",
        //                    "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                    "albumSize": 53,
        //                    "alias": [],
        //                    "trans": "",
        //                    "musicSize": 210,
        //                    "picId_str": "109951164017636713"
        //                },
        //                "songs": [],
        //                "alias": [],
        //                "status": 3,
        //                "copyrightId": 743010,
        //                "commentThreadId": "R_AL_3_34700845",
        //                "artists": [{
        //                    "name": "Flume",
        //                    "id": 361505,
        //                    "picId": 0,
        //                    "img1v1Id": 0,
        //                    "briefDesc": "",
        //                    "picUrl": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                    "img1v1Url": "http://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        //                    "albumSize": 0,
        //                    "alias": [],
        //                    "trans": "",
        //                    "musicSize": 0
        //                }],
        //                "img80x80": "https://p2.music.126.net/N9QDa3qlkRJ3IyFHVY07cg==/109951164430276051.jpg?param=80x80x1"
        //            }
        
    }
    class Playlist: Decodable {
        
        var id: Int
        
        var name: String

        
        var creator: NMUser
        
        var description: String?
        
        var coverImgUrl: URL?

        
        var tags: [String]?
        //            "playlist": {
        //                "name": "村上春树：与小泽征尔共度的午后音乐时光",
        //                "id": 2878246071,
        //                "trackNumberUpdateTime": 1562725681064,
        //                "status": 0,
        //                "userId": 1778923105,
        //                "createTime": 1562725532068,
        //                "updateTime": 1578548785111,
        //                "subscribedCount": 423,
        //                "trackCount": 22,
        //                "cloudTrackCount": 0,
        //                "coverImgUrl": "http://p2.music.126.net/ys9AuM50On9-cb1TRvTZuA==/109951164614286837.jpg",
        //                "coverImgId": 109951164614286837,
        //                "description": "就像爱一样，好音乐永远不嫌多。本歌单收录村上春树与小泽征尔的对谈集《与小泽征尔共度的午后音乐时光》所提及的音乐曲目。村上春树说，听音乐是这个世界上最高的喜悦。如果音乐类似一种能量传递，那么通过书，将这些乐曲分享给大家，希望能给读者与听众带来一些平静与欢愉。",
        //                "tags": ["欧美", "古典", "怀旧"],
        //                "playCount": 17420,
        //                "trackUpdateTime": 1578797765760,
        //                "specialType": 0,
        //                "totalDuration": 0,
        //                "creator": {
        //                    "defaultAvatar": false,
        //                    "province": 110000,
        //                    "authStatus": 1,
        //                    "followed": false,
        //                    "avatarUrl": "http://p1.music.126.net/3vEchhPmzoi8IQbNLuCRMQ==/109951164019593333.jpg",
        //                    "accountStatus": 0,
        //                    "gender": 1,
        //                    "city": 110101,
        //                    "birthday": -2209017600000,
        //                    "userId": 1778923105,
        //                    "userType": 10,
        //                    "nickname": "云音乐古典之声",
        //                    "signature": "这里是云音乐的古典之声官方号，定期推送最新最经典的古典音乐内容，不定期推出古典黑胶唱片等相关福利，欢迎关注和勾搭！喜欢古典音乐的用户还可以在移动端首页发现并进入古典专区，畅享更多古典内容哦！",
        //                    "description": "云音乐古典新歌推荐官方账号",
        //                    "detailDescription": "云音乐古典新歌推荐官方账号",
        //                    "avatarImgId": 109951164019593333,
        //                    "backgroundImgId": 109951162868126486,
        //                    "backgroundUrl": "http://p1.music.126.net/_f8R60U9mZ42sSNvdPn2sQ==/109951162868126486.jpg",
        //                    "authority": 0,
        //                    "mutual": false,
        //                    "expertTags": null,
        //                    "experts": null,
        //                    "djStatus": 0,
        //                    "vipType": 11,
        //                    "remarkName": null,
        //                    "backgroundImgIdStr": "109951162868126486",
        //                    "avatarImgIdStr": "109951164019593333",
        //                    "avatarImgId_str": "109951164019593333"
        //                },
        //                "tracks": null,
        //                "subscribers": [],
        //                "subscribed": null,
        //                "commentThreadId": "A_PL_0_2878246071",
        //                "newImported": false,
        //                "adType": 0,
        //                "highQuality": false,
        //                "privacy": 0,
        //                "ordered": false,
        //                "anonimous": false,
        //                "coverImgId_str": "109951164614286837"
        //            }
    }
    
    
    class Show: Decodable {

        var id: Int
        var title: String
        
        var url: URL?
        var cover: URL?
        
        var time: [Date]
    }
    class Radio: Decodable {
        
        var id: UInt
        var name: String
        
        var categoryId: Int
        var category: String
        
        var dj: NMUser?

        var picUrl: URL?
        var desc: String?

//        "subCount": 638383,
//        "programCount": 731,
//        "createTime": 1470473162542,
//        "radioFeeType": 0,
//        "feeScope": 0,
//        "buyed": false,
//        "videos": null,
//        "finished": false,
//        "underShelf": false,
//        "purchaseCount": 0,
//        "price": 0,
//        "originalPrice": 0,
//        "discountPrice": null,
//        "lastProgramCreateTime": 1583242222499,
//        "lastProgramName": null,
//        "lastProgramId": 2065843095,
//        "picId": 109951163620386920,
//        "rcmdText": "用声音，治愈每一个孤独患者",
//        "composeVideo": false,
//        "img80x80": "http://p1.music.126.net/Jbno74p63uXXIgtafwMZcw==/109951163620386920.jpg?param=80x80x1"
    }
    class Program: Decodable {
        
        var id: UInt
        var name: String
        
        var coverUrl: URL?

//        var dj: NMUser
        //var radio: Any?
        
//        var channels: [String]
        
        var radio: Radio
        
        
        var description: String?
        //var img80x80: URL?
    }
    
    class Video: Decodable {
        
        var id: UInt
        var status: Int // 0 normal, -1 removed
        
        var url: URL?
        var cover: URL?
        
        var size: CGSize
        var duration: TimeInterval
        
        var creator: NMUser?

        var plays: Int
        
    //    {
    //        "vid": 0,
    //        "playTime": 5934,
    //        "size": 424649,
    //        "state": 1,
    //        "coverType": 0,
    //        "urlInfo": null,
    //        "threadId": null,
    //        "videoStatus": 0,
    //        "resolutions": [{
    //            "resolution": 0,
    //            "size": 424649
    //        }, {
    //            "resolution": 0,
    //            "size": 632156
    //        }, {
    //            "resolution": 0,
    //            "size": 1020487
    //        }],
    //        "antisChecking": false,
    //    }
        
        required init(from decoder: Decoder) throws {
            
            let container = try decoder.container(keyedBy: CustomKey.self)
            
            self.id = try container.decode(UInt.self, forKey: "vid")
            self.status = try container.decode(Int.self, forKey: "videoStatus")

            self.url = try URL(string: "nm://music.163.com/url/video?id=\(container.decode(String.self, forKey: "videoId"))")
            self.cover = try container.decodeIfPresent(URL.self, forKey: "coverUrl")
            self.duration = try container.decode(TimeInterval.self, forKey: "durationms") / 1000
            
            self.size = .init(width: try container.decodeIfPresent(CGFloat.self, forKey: "width") ?? 0,
                              height: try container.decodeIfPresent(CGFloat.self, forKey: "height") ?? 0)
            
            self.creator = try container.decodeIfPresent(NMUser.self, forKey: "creator")
            
            self.plays = try container.decode(Int.self, forKey: "playTime")
        }
    }
    class Movie: Decodable {
        
        var id: UInt
        var name: String
        var status: Int // 0 normal, -1 removed

        var url: URL?
        var cover: URL?
        
        var size: CGSize
        var duration: TimeInterval
        
        var artist: Artist
        
        var plays: Int

    //    {
    //        "status": 0,
    //        "artist": {
    //            "name": "颜人中",
    //            "id": 31376161,
    //            "picId": 0,
    //            "img1v1Id": 18686200114669622,
    //            "briefDesc": "",
    //            "picUrl": "http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
    //            "img1v1Url": "http://p4.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg",
    //            "albumSize": 0,
    //            "alias": [],
    //            "trans": "",
    //            "musicSize": 0,
    //            "topicPerson": 0,
    //            "img1v1Id_str": "18686200114669622"
    //        },
    //        "artistName": "颜人中",
    //        "imgurl": "http://p4.music.126.net/zkwWeyMDCsHshV2_qNOiVw==/109951164710896672.jpg",
    //        "imgurl16v9": "http://p4.music.126.net/UDcCfpwl6mwE0FSgvxsIeA==/109951164710898573.jpg",
    //        "artists": [{
    //            "id": 31376161,
    //            "name": "颜人中"
    //        }],
    //        "urlInfo": null,
    //        "price": 0,
    //        "duration": 386000,
    //        "playCount": 367895,
    //    }
        
        required init(from decoder: Decoder) throws {
            
            let container = try decoder.container(keyedBy: CustomKey.self)
            
            self.id = try container.decode(UInt.self, forKey: "id")
            self.name = try container.decode(String.self, forKey: "name")
            self.status = try container.decode(Int.self, forKey: "status")

            self.url = URL(string: "nm://music.163.com/url/movie?id=\(self.id)")
            self.cover = try container.decode(URL.self, forKey: "imgurl16v9") // imgurl
            self.duration = try container.decode(TimeInterval.self, forKey: "duration") / 1000
            
            self.size = .init(width: try container.decodeIfPresent(CGFloat.self, forKey: "width") ?? 0,
                              height: try container.decodeIfPresent(CGFloat.self, forKey: "height") ?? 0)
            
            self.artist = try container.decode(Artist.self, forKey: "artist")
            
            self.plays = try container.decode(Int.self, forKey: "playCount")
        }
    }
    
    
    class Log: Decodable {
        
        var id: String
        var type: Int = 1  /* 1 normal, 2 video */
        
        var size: CGSize
        var cover: URL?
        var contents: String
        
        var sender: NMUser
        
        
        required init(from decoder: Decoder) throws {
            
            let container = try decoder.container(keyedBy: CustomKey.self)
            let base = try container.nestedContainer(keyedBy: CustomKey.self, forKey: "mlogBaseData")
            
            self.id = try base.decode(String.self, forKey: "id")
            self.type = try base.decode(Int.self, forKey: "type")

            self.contents = try base.decode(String.self, forKey: "text")
            self.cover = try base.decode(URL.self, forKey: "coverUrl")
            self.size = .init(width: try base.decode(CGFloat.self, forKey: "coverWidth"),
                              height: try base.decode(CGFloat.self, forKey: "coverHeight"))

            self.sender = try container.decode(NMUser.self, forKey: "userProfile")
            
            // mlogExtVO
            // mlogDetail
        }
    }
    
    // program 节目
    
    class Quoted: Decodable {
        
        var status: Int

        var sender: NMUser
        
        var contents: String
        var expression: URL?

        var replies: [Quoted]?
        
        required init(from decoder: Decoder) throws {
            
            let container = try decoder.container(keyedBy: CustomKey.self)
            
            self.status = try container.decode(Int.self, forKey: "status")

            self.contents = try container.decode(String.self, forKey: "content")
            self.expression = try container.decodeIfPresent(URL.self, forKey: "expressionUrl")

            self.sender = try container.decode(NMUser.self, forKey: "user")
            
            self.replies = try container.decodeIfPresent([Quoted].self, forKey: "beReplied")
        }
    }
    
    
    class Topic: Decodable {
        
        var id: UInt
        
        var title: String
        
        var coverUrl: URL?
        var rectanglePicUrl: URL?

        var creator: NMUser
        
        // var topic: Subject?
    }
    
    class Webpage: Decodable {
        
        var id: String
        
        var title: String
        var subTitle: String
        
        var coverImgUrl: URL?
        var webviewUrl: URL?
    }

}
